package org.ym.demo.dao.impl;

import java.sql.SQLException;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.ym.demo.dao.IUserDao;
import org.ym.demo.dao.model.UserInfo;

/**
 * IUserDao实现
 * 
 * @author Administrator
 * 
 */
@Component
public class IUserDaoImpl implements IUserDao {

	private HibernateTemplate hibernateTemplate;

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Override
	public void saveUser(UserInfo user) throws SQLException {
		hibernateTemplate.save(user);
	}

	@Override
	public void delUser(UserInfo user) throws SQLException {
		hibernateTemplate.delete(user);
	}

	@Override
	public void editUsre(UserInfo user) throws SQLException {
		hibernateTemplate.update(user);
		hibernateTemplate.execute(new HibernateCallback<Object>() {

			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				return null;
			}
		});
	}

	@Override
	public UserInfo getUserById(int id) throws SQLException {
		UserInfo user = (UserInfo) hibernateTemplate.get(UserInfo.class, id);
		return user;
	}

}
